package com.voxel.sense.infomsg.widget.map.adpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.Marker;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.MissionLocationEntity;
import cyy.example.com.facotry.model.api.response.WarningFirmEntity;

import com.voxel.sense.infomsg.data.bean.MakerInfoBean;
import com.voxel.sense.infomsg.utils.marker.cluster.data.ClusterItem;

/**
 * Created by ChenYiYao on 2017/12/9.
 */

public class MarkerInfoWinowAdapter implements AMap.InfoWindowAdapter {

    private Marker mCurrentClickMarker; //记录当前的marker
    private Context mContext;
    private boolean isLoadDataComplete = false;//是否加载数据结束
    private boolean IsFirmTypeSenTask = false;//是否可以派发任务类型
    private Object mCurrentMarkerDetailData;
    private View.OnClickListener mClickListener;
    String flagID = "flagID";

    public MarkerInfoWinowAdapter(Context context) {
        mContext = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        String title = marker.getTitle();
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View inflate = null;
        //判断类型
        //判断是任务类型或者人员类型，分别获取不同的布局
        String currenId = getCurrentMarkerID(title);


        Object obj = marker.getObject();
        Object data;
        if (obj instanceof ClusterItem) {
            data = ((ClusterItem) obj).getObject();
        } else {
          return null;
        }

        if (Constants.MARKER_TYPE_TASK.equals(title)) { //任务类型
            inflate = layoutInflater.inflate(R.layout.dialog_misson, null);
            MissionLocationEntity.BodyBean flagBean =
                    (MissionLocationEntity.BodyBean) data;
            flagID = flagBean.getTaskNumber();
        } else if (Constants.MARKER_TYPE_PEOPLE.equals(title)) { //人员类型

        } else if (Constants.MARKER_TYPE_FIRM.equals(title)) { // 公司类型
//            inflate = layoutInflater.inflate(R.layout.item_map_firm_window, null);
//            inflate.setVisibility(View.VISIBLE);
//            FirmLocationEntity flag = (FirmLocationEntity) data;
//            flagID = flag.getCompanyName();
        } else if (Constants.MARKER_TYPE_WARN_OVER_FIRM.equals(title)) { //超标类型
            inflate = layoutInflater.inflate(R.layout.item_map_firm_window, null);
            inflate.setVisibility(View.VISIBLE);
            WarningFirmEntity.OutOfLimitCompanyBean flag = (WarningFirmEntity.OutOfLimitCompanyBean) data;
            flagID = flag.getCompanyId();
        } else if (IntentConstants.TASY_TYPE.equals(title)) { //
            inflate = layoutInflater.inflate(R.layout.item_map_location_window, null);
            inflate.setVisibility(View.VISIBLE);
//            WarningFirmEntity.BodyBean flag = (WarningFirmEntity.BodyBean) data;
            flagID = "1";
            return inflate;
        }
//        inflate = layoutInflater.inflate(R.layout.item_map_go_task, null);

        loadInfoWindow(marker, inflate, currenId, flagID);
        return inflate;
    }

    private void loadInfoWindow(Marker marker, View inflate, String currenId, String flagID) {
        //若当前选中的marker对象标记部位空，且网络加载的标记是isLoadDataComplete为ture
        //则表示数据已加载回来，这是隐藏空布局 band width
        if (mCurrentClickMarker != null && flagID != null && isLoadDataComplete
                && flagID.equals(currenId)) {
            //分化初始化任务和人员的控件
            inflate.findViewById(R.id.fl_empty).setVisibility(View.GONE);
            if (Constants.MARKER_TYPE_TASK.equals(marker.getTitle())) {
                loadMissionInfoWindow(inflate);
            } else if (Constants.MARKER_TYPE_PEOPLE.equals(marker.getTitle())) {
                loadPeopleInfoWindow(inflate);
            } else if (Constants.MARKER_TYPE_FIRM.equals(marker.getTitle())) {
                //加载公司信息窗口
                loadFirmInfoWindow(inflate, flagID);
            } else if (Constants.MARKER_TYPE_WARN_OVER_FIRM.equals(marker.getTitle())) {
                //加载公司信息窗口
                loadFirmInfoWindow(inflate, flagID);
            }
            //初始化完成后，改变数据状态改变
            isLoadDataComplete = false;
        }
        //加入创建任务
//        TextView goTask = inflate.findViewById(R.id.go_task);
//        goTask.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(mClickListener != null){
//                    Log.e("+++++++", "地图地址"+currenId);
//                    mClickListener.onClick(v);
//                }
//            }
//        });
    }

    //加载公司信息窗口
    private void loadFirmInfoWindow(View inflate, String flagID) {
        TextView tv_firm_base = inflate.findViewById(R.id.tv_firm_base);//基础数据
        TextView tv_illegal = inflate.findViewById(R.id.tv_illegal);//违规记录
        TextView tv_firm_detail = inflate.findViewById(R.id.tv_firm_detail);//实时数据
        View view_detail_divider = inflate.findViewById(R.id.view_detail_divider);//实时数据下的分割线
        TextView tv_mission = inflate.findViewById(R.id.tv_mission);//派发任务
        ConstraintLayout layout =  inflate.findViewById(R.id.ll_info);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mClickListener != null){

                    mClickListener.onClick(v);
                }
            }
        });
    }


    /**
     * 加载任务窗口信息
     */
    private void loadMissionInfoWindow(View inflate) {
        TextView tv_desc = inflate.findViewById(R.id.tv_describe);
        TextView tv_level = inflate.findViewById(R.id.tv_level);//等级
        TextView tv_state = inflate.findViewById(R.id.tv_state);//进行状态
        TextView tv_firm_name = inflate.findViewById(R.id.tv_firm_name);//公司名称
        TextView tv_address = inflate.findViewById(R.id.tv_address);//地址
        TextView tv_action = inflate.findViewById(R.id.tv_action);//进度
        TextView tv_begin_time = inflate.findViewById(R.id.tv_begin_time);//开始时间
        TextView tv_end_time = inflate.findViewById(R.id.tv_end_time);//结束时间
        TextView tv_people_name = inflate.findViewById(R.id.tv_people_name);//执行人

    }

    @SuppressLint("SetTextI18n")
    private void loadPeopleInfoWindow(View inflate) {

        TextView tv_phone = inflate.findViewById(R.id.tv_phone);
        TextView tv_team = inflate.findViewById(R.id.tv_team);//所在任务组
        ImageView iv_phone = inflate.findViewById(R.id.iv_phone);//电话
        TextView tv_action = inflate.findViewById(R.id.tv_action);//任务完成度
        TextView tv_name = inflate.findViewById(R.id.tv_name);//人员名称


    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    public Marker getCurrentClickMarker() {
        return mCurrentClickMarker;
    }

    public void setCurrentClickMarker(Marker currentClickMarker) {
        mCurrentClickMarker = currentClickMarker;
    }

    public boolean isLoadDataComplete() {
        return isLoadDataComplete;
    }

    public void setLoadDataComplete(boolean loadDataComplete) {
        isLoadDataComplete = loadDataComplete;
    }

    public Object getCurrentMarkerDetailData() {
        return mCurrentMarkerDetailData;
    }

    public String getCurrentMarkerID(String type) {
        String currenId = "currenId";
        if (mCurrentClickMarker == null)
            return currenId;
        if (Constants.MARKER_TYPE_PEOPLE.equals(type) ) {

        } else if ((Constants.MARKER_TYPE_FIRM.equals(type) || Constants.MARKER_TYPE_WARN_OVER_FIRM.equals(type))) {
        //超标或公司
            return flagID;
        }
        return currenId;
    }

    /**
     * 设置marker的info窗口数据源
     *
     * @param currentMarkerDetailData
     */
    public void setCurrentMarkerDetailData(Object currentMarkerDetailData) {
        mCurrentMarkerDetailData = currentMarkerDetailData;
    }

    public boolean isFirmTypeSenTask() {
        return IsFirmTypeSenTask;
    }

    public void setFirmTypeSenTask(boolean firmTypeSenTask) {
        IsFirmTypeSenTask = firmTypeSenTask;
    }

    public void setOnFirmWindowClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;

    }

}
